<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/23
 * Time: 17:29
 * 权限控制
 */

namespace app\mall\controller;
use app\Base as parentBase;
use app\common\model\CenterModel;
use app\common\model\ClientModel;

class Base extends parentBase
{

    public function __construct (){
        parent::__construct();
        $this->apiUrl = 'http://www.webplatform.com/';

    }

    public function checkToken($params){
        $model_Client = new ClientModel();
        $client = $model_Client->findOne(['appid'=>$params['appid'],'appsecret'=>$params['appsecret'],'status'=>1]);

        $data = [];
        if( $client ){
            $client = $client->toArray();
            $expire_time = $client['expire_time'];
            if( $expire_time <= TIME ){
//                $code = 402;
//                $msg = 'token is expired';
                //暂不做登录验证

                $token = json_decode($this->getToken($params),true);
                if($token['code']==200){
                    $code = 200;
                    $msg = 'token refresh success';
                    $data = $token['data']['client'];
                }else{
                    $code = 402;
                    $msg = 'token refresh fail';
                    $data = [];
                }
            }else{
                $code = 200;
                $data = $client;
                $msg = 'success';
            }
        }else{
            $code = 402;
            $msg = 'appid or appsecret is wrong';
        }
        return msg($code, $data, $msg);
    }

    public function getToken($params){
        $params['appid'] = $params['appid'];
        $params['appsecret'] = $params['appsecret'];
        $params['nonce'] = random_str(10);
        $params['timestamp'] = time();
        $params['key'] = $params['appsecret'];
        ksort($params);
        $params['signature'] = strtolower(md5(urldecode(http_build_query($params))));
        return curls( $this->apiUrl.'v1/token',$params,1);
    }





}